package com.zjy.pgcms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjy.pgcms.common.*;
import com.zjy.pgcms.dto.reservation.ReservationAddDTO;
import com.zjy.pgcms.dto.reservation.ReservationInfo;
import com.zjy.pgcms.dto.reservation.ReservationPage;
import com.zjy.pgcms.dto.reservation.ReservationUpdateDTO;
import com.zjy.pgcms.entity.Reservation;
import com.zjy.pgcms.exception.BusinessException;
import com.zjy.pgcms.service.ReservationService;
import com.zjy.pgcms.util.BaseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class ReservationController {
    @Resource
    private ReservationService reservationService;

    /**
     * 用户预订
     * @param request
     * @param dto
     * @return
     */
    @PostMapping("/reservation/do")
    public BaseResponse doReservation(HttpServletRequest request, ReservationAddDTO dto){
        Long petId = dto.getPetId();
        Long projectId = dto.getProjectId();
        Long userId = BaseUtil.getLoginUserId(request);
        Date time = dto.getTime();
        if (time.before(new Date())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"时间不能再今天之前");
        }
        Reservation reservation = new Reservation(userId,petId,projectId,time);
        reservation.setStatus(ReservationStatus.UN_CONFIRMED.getCode());

        boolean saveSuccess = reservationService.save(reservation);
        if (saveSuccess){
            return Result.success();
        }else {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
    }

    /**
     * 用户变更预订时间
     * @param request
     * @param dto
     * @return
     */
    @PostMapping("/reservation/update")
    public BaseResponse updateReservation(HttpServletRequest request, @RequestBody  ReservationUpdateDTO dto){
        Long petId = dto.getPetId();
        Date time = dto.getTime();
        if (time.before(new Date())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"时间不能再今天之前");
        }

        Long id = dto.getId();
        Reservation reservation = reservationService.getById(id);

        reservation.setPetId(petId);
        reservation.setTime(time);

        boolean success = reservationService.updateById(reservation);
        if (success){
            return Result.success();
        }else {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
    }

    /**
     * 用户取消预订
     * @param id
     * @return
     */
    @GetMapping("/reservation/cancel/{id}")
    public BaseResponse cancel(@PathVariable Long id){
        Reservation reservation = reservationService.getById(id);
        reservation.setStatus(ReservationStatus.CANCEL.getCode());
        boolean success = reservationService.updateById(reservation);
        return Result.success(success);
    }

    /**
     * 店家受理用户预约
     * @param id
     * @return
     */
    @GetMapping("/reservation/accept/{id}/{opcode}")
    public BaseResponse complete(@PathVariable("id") Long id, @PathVariable("opcode") int opcode){
        Reservation reservation = reservationService.getById(id);
        //已确认
        if (opcode == ReservationStatus.CONFIRMED.getCode()){
            reservation.setStatus(opcode);
        }
        //取消
        if (opcode == ReservationStatus.CANCEL_ADMIN.getCode()){
            reservation.setStatus(opcode);
        }
        //完成
        if (opcode == ReservationStatus.COMPLETED.getCode()){
            reservation.setStatus(opcode);
        }
        boolean success = reservationService.updateById(reservation);
        if (success){
            return Result.success();
        }else {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
    }

    /**
     * 用户获取自己的订单
     * @param request
     * @return
     */
    @GetMapping("/get/reservation/{status}")
    public BaseResponse getReservationByUser(HttpServletRequest request,@PathVariable("status") String status){
        Long userId = BaseUtil.getLoginUserId(request);
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId",userId);
        if (status.equals("ing")){
            params.put("ing",status);
        }if (status.equals("ok")){
            params.put("ok",status);
        }if (status.equals("notOk")){
            params.put("notOk",status);
        }if (status.equals("cancel")){
            params.put("cancel",status);
        }
        List<ReservationInfo> reservationInfos = reservationService.queryReservationByCondition(params);

        return Result.success(reservationInfos);

    }

    @GetMapping("/reservation/get/{status}/{page}/{size}")
    public BaseResponse getAllReservation(
            @PathVariable("status") int status,
            @PathVariable("page") int page,
            @PathVariable("size") int size){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("status",status);
            hashMap.put("currentPage",page);
            hashMap.put("pageSize",size);
            List<ReservationPage> reservationPages = reservationService.queryByPage(hashMap);
            return Result.success(reservationPages);
    }

    @GetMapping("/reservation/count/{status}")
    public BaseResponse getAllReservation(@PathVariable("status") int status){
       QueryWrapper queryWrapper = new QueryWrapper();
       queryWrapper.eq("status",status);
        long count = reservationService.count(queryWrapper);
        return Result.success(count);
    }

    @GetMapping("/get/reservation/all")
    public BaseResponse getAllReservation(){
        List<Reservation> list = reservationService.list();
        return Result.success(list);

    }

    @DeleteMapping("/reservation/delete/{id}")
    public BaseResponse deleteReservation(@PathVariable Long id){
        boolean isSuccess = reservationService.removeById(id);
        if (isSuccess){
            return Result.success();
        }else {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
    }

    @GetMapping("/reservation/get/user")
    public BaseResponse getUserReservation(HttpServletRequest request){
        Long userId = BaseUtil.getLoginUserId(request);
        List<ReservationInfo> reservationInfos = reservationService.queryReservationByUserId(userId);

        return Result.success(reservationInfos);
    }

    @GetMapping("/reservation/process/{id}/{status}")
    public BaseResponse process(@PathVariable("id") String id,@PathVariable("status") int status){
        long l = Long.parseLong(id);
        Reservation reservation = new Reservation();
        reservation.setId(l);
        reservation.setStatus(status);
        boolean b = reservationService.setStatus(l, status);
        if (b){
            return Result.success();
        }else {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
    }



}
